package com.easylinkin.linkappapi.keywork.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.keywork.dto.KeyworkListDto;
import com.easylinkin.linkappapi.keywork.entity.KeyworkList;
import com.easylinkin.linkappapi.keywork.mapper.KeyworkListMapper;
import com.easylinkin.linkappapi.keywork.service.KeyworkListService;
import com.easylinkin.linkappapi.keywork.vo.KeyworkListVo;
import com.easylinkin.linkappapi.manageinfo.entity.ManageInfo;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.tenant.sevice.LinkappTenantService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-16
 */
@Service
@Slf4j
public class KeyworkListServiceImpl extends ServiceImpl<KeyworkListMapper, KeyworkList> implements KeyworkListService {


    @Resource
    private RedissonClient redisson;

    @Autowired
    private LinkappUserContextProducer linkappUserContextProducer;

    @Resource
    private KeyworkListMapper keyworkListMapper;


    @Override
    public IPage<KeyworkListVo> selectPage(Page page, KeyworkList keyworkList) {
        keyworkList.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return keyworkListMapper.selectKeyworkList(page, keyworkList);
    }

    @Override
    public List<KeyworkListVo> selectList(KeyworkList keyworkList) {
        keyworkList.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return baseMapper.selectKeyworkList(keyworkList);
    }

    @Override
    public List<KeyworkListVo> selectListTree(KeyworkList keyworkList) {
        keyworkList.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        keyworkList.setIsUsed(1);
        keyworkList.setLevel(1); //查询父级分类
        List<KeyworkListVo> parentList = keyworkListMapper.selectKeyworkList(keyworkList);
        for (KeyworkListVo parent:parentList){
            KeyworkList childListearch = new KeyworkList();
            childListearch.setParentId(parent.getId());
            childListearch.setLevel(2); //查询子级作业
            childListearch.setIsUsed(1);
            List<KeyworkListVo> childList = keyworkListMapper.selectKeyworkList(childListearch);
            parent.setChildren(childList);
        }
        return parentList;
    }


    @Override
    public List<Map<String, Object>> selectIsUsedListMap() {
        return baseMapper.selectIsUsedListMap(linkappUserContextProducer.getNotNullCurrent().getTenantId());
    }

    @Override
    public int updateKeyworkList(List<KeyworkList> keyworkListList) {
        Integer returnParam = 0;
        for (KeyworkList keyworkList:keyworkListList){
            KeyworkList tmpList = baseMapper.selectById(keyworkList.getId());
            tmpList.setIsUsed(keyworkList.getIsUsed()); // 修改是否使用字段
            tmpList.setUpdateTime(new Date());
            returnParam =  baseMapper.updateById(tmpList);


            //TODO 重新将更新分类的isused字段
            KeyworkList list = new KeyworkList();
            list.setIsUsed(1);
            list.setLevel(2);
            Set<Integer> isUsedParentSet = new HashSet<>();

        }
        return returnParam;
    }

}
